package com.uhylab.uhuvinetest.camera;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

/**
 * 项目名称：ViewFindView
 * 类描述：
 * 创建人：xyb
 * 创建时间：16/4/7 14:01
 * 修改人：xyb
 * 修改时间：16/4/7 14:01
 * 修改备注：
 */
public class ViewFindView extends View {
    /**
     * 画笔对象的引用
     */
    private Paint mPaint;
    /**
     * 四个绿色边角对应的长度
     */
    private int ScreenRate;

    /**
     * 四个绿色边角对应的宽度
     */
    private static final int CORNER_WIDTH = 10;
    /**
     * 扫描框中的中间线的与扫描框左右的间隙
     */
    private static final int MIDDLE_LINE_PADDING = 5;
    /**
     * 手机的屏幕密度
     */
    private static float density;
    private int width;
    private int height;
    private boolean _top, _right, _left, _bottom;

    /**
     * 中间的扫描框
     */
//    private Rect frame;
    public ViewFindView(Context context) {
        super(context);
        initView(context);
    }


    public ViewFindView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ViewFindView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public ViewFindView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context);
    }

    private void initView(Context context) {
        density = context.getResources().getDisplayMetrics().density;
        //将像素转换成dp
        ScreenRate = (int) (20 * density);
        mPaint = new Paint();
        //获取屏幕的宽和高
        width = context.getResources().getDisplayMetrics().widthPixels;
        height = context.getResources().getDisplayMetrics().heightPixels;

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {


//        Log.e("ARG",width+""+height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mPaint.setARGB(96, 0, 0, 0);
        float left = (float) (width * 1.0 / 10);
        float top = (float) (height * 2.0 / 12);
        float bottom = (float) ((height * 10 * 1.0) / 12);
        float right = (float) ((width * 9.0 * 1.0) / 10);
        //画出扫描框外面的阴影部分，共四个部分，扫描框的上面到屏幕上面，扫描框的下面到屏幕下面
        //扫描框的左边面到屏幕左边，扫描框的右边到屏幕右边
        canvas.drawRect(0, 0, width, top, mPaint);
        canvas.drawRect(0, top, left, bottom, mPaint);
        canvas.drawRect(right, top, width, bottom,
                mPaint);
        canvas.drawRect(0, bottom, width, height, mPaint);


        //画扫描框边上的角，总共8个部分
        mPaint.setColor(Color.RED);
        canvas.drawRect(left, top, left + ScreenRate,
                top + CORNER_WIDTH, mPaint);
        canvas.drawRect(left, top, left + CORNER_WIDTH, top
                + ScreenRate, mPaint);
        canvas.drawRect(right - ScreenRate, top, right,
                top + CORNER_WIDTH, mPaint);
        canvas.drawRect(right - CORNER_WIDTH, top, right, top
                + ScreenRate, mPaint);
        canvas.drawRect(left, bottom - CORNER_WIDTH, left
                + ScreenRate, bottom, mPaint);
        canvas.drawRect(left, bottom - ScreenRate,
                left + CORNER_WIDTH, bottom, mPaint);
        canvas.drawRect(right - ScreenRate, bottom - CORNER_WIDTH,
                right, bottom, mPaint);
        canvas.drawRect(right - CORNER_WIDTH, bottom - ScreenRate,
                right, bottom, mPaint);


        //探测的四条线
        if (_top) {
            canvas.drawRect(left,
                    top,
                    right,
                    top + CORNER_WIDTH, mPaint);
        }

        if (_left) {
            canvas.drawRect(left,
                    top,
                    left + CORNER_WIDTH,
                    bottom, mPaint);
        }


        if (_bottom) {
            canvas.drawRect(left,
                    bottom - CORNER_WIDTH,
                    right,
                    bottom, mPaint);
        }


        if (_right) {
            canvas.drawRect(right - CORNER_WIDTH,
                    top,
                    right,
                    bottom, mPaint);
        }


        _left = false;
        _right = false;
        _top = false;
        _bottom = false;
    }


    public void findViewInvalidate(boolean left, boolean right, boolean top, boolean bottom) {
        _left = left;
        _right = right;
        _top = top;
        _bottom = bottom;
        postInvalidate();
    }
}
